<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="500px"
    @close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="100px" size="small">
      <el-form-item label="标签名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入标签名称" @keyup.enter.native="handleSubmit"></el-input>
      </el-form-item>

      <el-form-item label="标签颜色" prop="color">
        <el-color-picker v-model="form.color"></el-color-picker>
      </el-form-item>
                    
      <el-form-item label="父标签" v-if="showParentSelect">
        <el-select v-model="form.parentId" clearable placeholder="请选择父标签">
          <el-option
            v-for="tag in parentTags"
            :key="tag.id"
            :label="tag.name"
            :value="tag.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="没有子标签">
        <el-switch v-model="form.isLeaf"></el-switch>
      </el-form-item>

      <el-form-item label="是否启用">
        <el-switch v-model="form.enabled"></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import api from '@/common/api'

export default {
  name: 'TagEditDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    tag: {
      type: Object,
      default: () => ({})
    },
    tcid: {
      type: Number,
      required: true
    },  
    parentTags: {
      type: Array,
      default: () => []
    },
    showParentSelect: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      form: {
        id: undefined,
        name: '',
        color: '#366ef4',
        parentId: undefined,
        isLeaf: true,
        enabled: true,
        gid: undefined,
        tcid: undefined
      },
      rules: {
        name: [
          { required: true, message: '请输入标签名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],       
      }
    }
  },
  computed: {
    dialogTitle() {
      return this.form.id ? '编辑标签' : '添加标签'
    }
  },
  watch: {
    visible(val) {
      this.dialogVisible = val
      if (val) {
        this.initForm()
      }
    },
    dialogVisible(val) {
      if (!val) {
        this.$emit('update:visible', false)
      }
    }
  },
  methods: {
    initForm() {
      if (this.tag.id) {
        this.form = { ...this.tag }
      } else {
        this.form = {
          id: undefined,
          name: '',
          color: '#366ef4',
          parentId: this.tag?.parentId,
          isLeaf: true,
          enabled: true,
          gid: undefined,
          tcid: this.tcid
        }
      }
    },
    handleClose() {
      this.$refs.form?.resetFields()
      this.dialogVisible = false
    },
    handleSubmit() {
      this.$refs.form.validate(async valid => {
        if (!valid) return

        this.loading = true
        try {
          const method = this.form.id ? 'admin_updateTag' : 'admin_addTag'
          await api[method](this.form)
          this.$message.success(this.form.id ? '更新成功' : '添加成功')
          this.$emit('success')
          this.handleClose()
        } catch (error) {
          this.$message.error(error.message || '操作失败')
        } finally {
          this.loading = false
        }
      })
    }
  }
}
</script>

<style scoped>
.el-color-picker {
  vertical-align: middle;
}
</style>
